﻿namespace LeadMaster.Helpers
{
    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;
    using System.Web.Security;
    using LeadMaster.Helpers;
    using System.Collections.Generic;
    using LeadMaster.Models;
    using System.Data;
    using LeadMaster.Properties;

    public static class POprocessing
    {
        private static DunsmoreContext db = new DunsmoreContext();

        public static string GeneratePID(int callCentreID)
        {
            CallCentres callCentre = db.CallCentres.Find(callCentreID);

            int keyval=++callCentre.pokey ?? 1;
            callCentre.pokey=keyval;
            string pid = "DM" + keyval.ToString() + callCentre.ccsuffix;
            db.Entry(callCentre).State = EntityState.Modified;
            db.SaveChanges();
            return pid;
        }

        public static void AllocatePO(int leadID, int callCentreID, int numOfCallcentres, bool extension=false)
        {
            Leads lead = db.Leads.Find(leadID);
            DateTime commenceDate=DateTime.Now;
            //if (extension == true && db.PurchaseOrders.Where(m => m.LeadID==leadID && m.CallCentreID == callCentreID).Count()>0)
            //{
            //  commenceDate = db.PurchaseOrders.Where(m => m.LeadID == leadID && m.CallCentreID == callCentreID).OrderByDescending(m => m.PurchaseOrderID)
            //                .Select(m => m.WeekEnding)
            //                .FirstOrDefault()
            //                .Value
            //                .AddDays(1);
            //}
            //else if (extension == true && db.PurchaseOrders.Where(m => m.LeadID==leadID).Count()>0)
            //{
            //  // more call centers must have been bought in
            //  commenceDate = db.PurchaseOrders.Where(m => m.LeadID == leadID).OrderByDescending(m => m.PurchaseOrderID)
            //                .Select(m => m.WeekEnding)
            //                .FirstOrDefault()
            //                .Value
            //                .AddDays(1);
            //}
            //else
            //{
            //  commenceDate = lead.CommencementDate;
            //}
            for (int i = 0; i < lead.TotalWeeks; i++)
            {
                PurchaseOrders purchaseOrder = new PurchaseOrders();
                purchaseOrder.LeadID = leadID;
                purchaseOrder.Quantity = 999;
                purchaseOrder.PID = GeneratePID(callCentreID);
                purchaseOrder.CallCentreID = callCentreID;
                purchaseOrder.Deficit = 999;
                purchaseOrder.Accepted = 0;
                purchaseOrder.InvalidReturns = 0;
                purchaseOrder.Rejected = 0;
                purchaseOrder.Replaced = 0;
                purchaseOrder.WeekBeginning = commenceDate.AddDays(7 * i);
                purchaseOrder.WeekEnding = purchaseOrder.WeekBeginning.Value.AddDays(6);
                db.PurchaseOrders.Add(purchaseOrder);
            }
            db.SaveChanges();
        }
    }
}